﻿import QtQuick 2.0

Rectangle{

    property string defaultIconSource
    property string doneIconSource
    property string doingIconSource
    property double currentValue
    property string unit: isangle ? "°" : "cm"

    property int doingTimer: 0
    property int doingLimit: 5
    property bool isangle: false

    anchors.horizontalCenter: parent.horizontalCenter
    width: 142
    height: 53
    color: "#282828"
    radius: height/2
    border.color: "#8C8C8C"
    border.width: 0

    Image {
        id: icon
        source: defaultIconSource
        anchors.verticalCenter: parent.verticalCenter
        anchors.left: parent.left
        anchors.leftMargin: 20

    }

    Text {
        id: valueText
        anchors.verticalCenter: parent.verticalCenter
        anchors.right: parent.right
        anchors.rightMargin: 40
        text: String(currentValue)
        color: "grey"
        font{
            family: "Microsoft YaHei"
            pixelSize: 28
        }
    }

    Text {
        id: unitText
        anchors.verticalCenter: parent.verticalCenter
        anchors.left: valueText.right
        anchors.leftMargin: 5
        text: unit
        color: currentValue === 0.0 ? "grey" : "white"
        font{
            family: "Microsoft YaHei"
            pixelSize: 14
        }
    }

    onCurrentValueChanged: {
        doingTimer = 0
        if(!time.running){
            time.start()
        }
        width = 200
        height = 75
        radius = height/2
        border.width = 2
        icon.source = ""
        icon.source = doingIconSource
        valueText.text = String(currentValue)
        valueText.color = "#00FFFF"
        unitText.color = "#00FFFF"
    }

    onDoingTimerChanged: {
        if(doingTimer > doingLimit){
            width = 142
            height = 53
            radius = height/2
            border.width = 0
            icon.source = ""
            icon.source = doneIconSource
            valueText.color = "white"
            unitText.color = "white"
            time.stop()
        }
    }

    Timer {
        id: time
        interval: 1000
        repeat: true
        triggeredOnStart: true
        onTriggered: {
            doingTimer+=1
        }
    }
}
